package cn.manjuu.touhou.engine;

import java.util.List;

import android.text.Html;
import cn.manjuu.touhou.activity.DramaActivity;
import cn.manjuu.touhou.db.dao.DramaDao;
import cn.manjuu.touhou.domain.Drama;
import cn.manjuu.touhou.interfaces.IConstantValues;

public class DramaEngine {

	private static DramaEngine mInstance;
	private DramaActivity mActivity;
	private List<Drama> mDramas;
	private DramaDao dao;
	private int mIndex;

	public void setActivity(DramaActivity activity) {
		this.mActivity = activity;
		dao = new DramaDao(mActivity);
	}

	public static DramaEngine getInstance() {
		if (null == mInstance) {
			mInstance = new DramaEngine();
		}
		return mInstance;
	}

	private DramaEngine() {
		super();
	}

	public void initTheme(String theme) {
		// TODO Auto-generated method stub
		mIndex = 0;
		mDramas = dao.queryAllOrderIndex(DramaDao.THEME, theme);
	}

	public boolean next() {
		if (mDramas.size() == mIndex) {
			return false;
		}
		nextImage();
		nextText();
		mIndex++;
		return true;
	}

	private void nextImage() {
		// TODO Auto-generated method stub
		String image = mDramas.get(mIndex).getImage();
		if(null == image) {
			return;
		}
		int id = mActivity.getResources().getIdentifier(
				image, "drawable",
				IConstantValues.PACKAGE_NAME);
		mActivity.getIv_drama_character().setImageResource(id);
	}

	private void nextText() {
		// TODO Auto-generated method stub
		mActivity.getTv_drama_content().setText(
				Html.fromHtml(mDramas.get(mIndex).getContent()));
	}
}
